//
//  StatusCell.swift
//  weibo
//
//  Created by 陈伟 on 15/10/19.
//  Copyright © 2015年 陈伟. All rights reserved.
//

import UIKit

let statusCellMargin: CGFloat = 12
let statusCellconWidth: CGFloat = 35

class StatusCell: UITableViewCell {
    
    //给cell设置值
    var statusModel: StatusViewModel? {
        didSet {
            topView.viewModel = statusModel
            pictureView.viewModel = statusModel!
            statusTextLabel.text = statusModel?.status.text
            pictureView.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(pictureView.bounds.width)
                make.height.equalTo(pictureView.bounds.height)
//                let offset = statusModel?.thumbnailUrls?.count > 0 ? statusCellMargin : 0
//                make.top.equalTo(statusTextLabel.snp_bottom).offset(offset)
            }
        }
    }
    
    //通过模型来设置宽高的
    func rowHeight(vm: StatusViewModel) -> CGFloat {
        statusModel = vm
        //更新约束必备
        contentView.layoutIfNeeded()
        //返回bottom的最大Y值
        return CGRectGetMaxY(bottomView.frame)
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .None
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var topView:StatusCellTopView = StatusCellTopView()
    lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    lazy var statusTextLabel: UILabel = UILabel(title: "正文", screenInset: statusCellMargin)
    lazy var pictureView: StatusPictureView = StatusPictureView()
    
    func setupUI() {
        
        //顶部视图
        contentView.addSubview(topView)
        
        //底部视图
        contentView.addSubview(bottomView)
        
        //描述文字
        contentView.addSubview(statusTextLabel)
        
        //中间配图
        contentView.addSubview(pictureView)
        
        //        topView.backgroundColor = UIColor.init(white: 0.8, alpha: 1)
        //        statusTextLabel.sizeToFit()
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * statusCellMargin + statusCellconWidth)
        }
        
        statusTextLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left).offset(statusCellMargin)
//            make.right.equalTo(contentView.snp_right).offset(-statusCellMargin)
            make.top.equalTo(topView.snp_bottom).offset(statusCellMargin)
        }
        
//        pictureView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(statusTextLabel.snp_bottom).offset(statusCellMargin)
//            make.left.equalTo(statusTextLabel.snp_left)
//            make.width.equalTo(300)
//            make.height.equalTo(90)
//        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.height.equalTo(44)
            make.top.equalTo(pictureView.snp_bottom).offset(statusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
//            make.bottom.equalTo(contentView.snp_bottom)
        }
    }
}
